package com.travel.rabbit.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.travel.rabbit.dao.AdminRabbitDao;
import com.travel.rabbit.dao.CooperativeSpecialtyMerchantsDao;
import com.travel.rabbit.entity.AdminRabbit;
import com.travel.rabbit.entity.CooperativeSpecialtyMerchants;
import com.travel.rabbit.enums.BusinessEnum;
import com.travel.rabbit.enums.ConstantStateEnum;
import com.travel.rabbit.enums.DeleteEnum;
import com.travel.rabbit.enums.MerchantStatusEnum;
import com.travel.rabbit.enums.UniqueCodeEnum;
import com.travel.rabbit.service.CooperativeSpecialtyMerchantsService;
import com.travel.rabbit.entity.Result;
import com.travel.rabbit.utils.StringCustomizedUtils;
import com.travel.rabbit.dto.CooperativeSpecialtyMerchantsDTO;
import com.travel.rabbit.dto.MerchantsStatusDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * (CooperativeSpecialtyMerchants)表服务实现类
 *
 * @author makejava
 * @since 2021-12-30 15:43:26
 */
@Service("cooperativeSpecialtyMerchantsService")
public class CooperativeSpecialtyMerchantsServiceImpl implements CooperativeSpecialtyMerchantsService {

    @Value("${system.admin}")
    private String admin;

    @Resource
    private CooperativeSpecialtyMerchantsDao merchantsDao;

    @Resource
    private AdminRabbitDao adminRabbitDao;

    @Override
    public Result add(CooperativeSpecialtyMerchantsDTO merchantsVO) {
        CooperativeSpecialtyMerchants checkExist = merchantsDao.selectOne(Wrappers.<CooperativeSpecialtyMerchants>lambdaQuery()
                .eq(CooperativeSpecialtyMerchants::getCityId, merchantsVO.getCityId())
                .eq(CooperativeSpecialtyMerchants::getMerchantsName, merchantsVO.getMerchantsName())
                .eq(CooperativeSpecialtyMerchants::getIsDelete, DeleteEnum.NORMAL.getCode()));
        if (!Objects.isNull(checkExist)){
            return Result.error(BusinessEnum.MERCHANT_EXIST.getCode(),BusinessEnum.MERCHANT_EXIST.getMsg());
        }

        CooperativeSpecialtyMerchants info = new CooperativeSpecialtyMerchants();
        BeanCopier beanCopier = BeanCopier.create(CooperativeSpecialtyMerchantsDTO.class,CooperativeSpecialtyMerchants.class,false);
        beanCopier.copy(merchantsVO,info,null);
        info.setMerchantsCode(StringCustomizedUtils.generatorUniqueCodeByType(UniqueCodeEnum.MERCHANT.getMsg()));
        info.setMerchantsStatus(MerchantStatusEnum.NORMAL.getCode());
        info.setMerchantsStar(ConstantStateEnum.NORMAL.getCode());
        info.setIsDelete(DeleteEnum.NORMAL.getCode());
        info.setPioneer(StringUtils.isBlank(merchantsVO.getPioneer())? admin : merchantsVO.getPioneer());
        info.setCreateBy(merchantsVO.getOptBy());
        info.setUpdateBy(merchantsVO.getOptBy());
        info.setCreateTime(new Date());
        info.setUpdateTime(new Date());
        return Result.success(merchantsDao.insert(info));
    }

    @Override
    public Result update(CooperativeSpecialtyMerchantsDTO merchantsVO) {
        CooperativeSpecialtyMerchants checkExist = merchantsDao.selectOne(Wrappers.<CooperativeSpecialtyMerchants>lambdaQuery()
                .eq(StringUtils.isNotBlank(merchantsVO.getCityId()),CooperativeSpecialtyMerchants::getCityId,merchantsVO.getCityId())
                .eq(CooperativeSpecialtyMerchants::getMerchantsName, merchantsVO.getMerchantsName())
                .eq(CooperativeSpecialtyMerchants::getIsDelete, DeleteEnum.NORMAL.getCode()));
        if (!Objects.isNull(checkExist)){
            return Result.error(BusinessEnum.MERCHANT_EXIST.getCode(),BusinessEnum.MERCHANT_EXIST.getMsg());
        }
        CooperativeSpecialtyMerchants info = new CooperativeSpecialtyMerchants();
        BeanCopier beanCopier = BeanCopier.create(CooperativeSpecialtyMerchantsDTO.class,CooperativeSpecialtyMerchants.class,false);
        beanCopier.copy(merchantsVO,info,null);
        info.setUpdateBy(merchantsVO.getOptBy());
        info.setUpdateTime(new Date());
        int update = merchantsDao.update(info);
        return Result.success();
    }

    @Override
    public Result updateMerchantsStatus(MerchantsStatusDTO merchantsVO) {
        AdminRabbit adminRabbit = adminRabbitDao.selectOne(Wrappers.<AdminRabbit>lambdaQuery()
                .eq(AdminRabbit::getUserCode, merchantsVO.getOptBy()));
        if (Objects.isNull(adminRabbit) || !admin.equals(merchantsVO.getOptBy())){
            return Result.error(BusinessEnum.USER_UNAUTHORIZED.getCode(),BusinessEnum.USER_UNAUTHORIZED.getMsg());
        }
        Date date = new Date();
        CooperativeSpecialtyMerchants info = new CooperativeSpecialtyMerchants();
        info.setMerchantsCode(merchantsVO.getMerchantsCode());
        info.setMerchantsStatus(merchantsVO.getMerchantStatus());
        info.setUpdateStatusDate(date);
        info.setUpdateTime(date);
        info.setUpdateBy(merchantsVO.getOptBy());
        merchantsDao.update(info);
        return Result.success();
    }

    @Override
    public Result closeMerchants(MerchantsStatusDTO merchantsVO) {
        return null;
    }

    @Override
    public Result queryMerchantByNo(String merchantsNo) {
        return Result.success(merchantsDao.selectOne(Wrappers.<CooperativeSpecialtyMerchants>lambdaQuery()
                .eq(CooperativeSpecialtyMerchants::getMerchantsCode,merchantsNo)
                .in(CooperativeSpecialtyMerchants::getMerchantsStatus, MerchantStatusEnum.queryMerchantStatus())
                .eq(CooperativeSpecialtyMerchants::getIsDelete, DeleteEnum.NORMAL.getCode()))
        );
    }
}